<?php
/**
 * The model of a blog post which corresponds to the table "posts"
 *
 * @todo describe what hook injects this
 * 
 * @package core
 * @version $Id: BlogPost.php 50 2010-05-01 12:10:33Z flavius.as $
 * @copyright 2010 &copy; Flavius Aspra
 * @author Flavius Aspra <flav@yet-another-project.com> 
 * @license {@link http://yii-modules.yet-another-project.com/licence}
 */
class BlogPost extends ActiveRecord {
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}
	public function rules() {
		return array(
			array('title,content,status','required'),
		);
	}
	public function tableName() {
		return 'posts';
	}
	/**
	 * @todo document the events raised to get relations
	 */
	public function relations() {
		//todo belongs_to User
		//todo has_many tags
		return array();
	}
	/**
	 * get the introductory paragraph based on its content
	 *
	 * @todo is there any way to specify "intro" as a meta AR column?
	 * @return string the text
	 */
	public function getIntro($stripTags=FALSE) {
		if(!empty($this->content)) {
			preg_match('/<p>.+<\/p>/',$this->content,$result);
			if(!$stripTags) {
				return array_pop($result);
			}
			else {
				return strip_tags(array_pop($result));
			}
		}
		else {
			return NULL;
		}
	}
	public function scopes() {
		return array(
			'published' => array(
				'condition' => 'status=0',
			),
		);
	}
}
